<template>
	<view>
		<view class="content">
			<view class="row">
				<view class="nominal">
					收件人
				</view>
				<view class="input">
					<input placeholder="请输入收件人姓名" type="text" v-model="model.userName" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					电话号码
				</view>
				<view class="input">
					<input placeholder="请输入收件人电话号码" type="text" v-model="model.phone" />
					<button open-type="getPhoneNumber" style="font-weight: 200;
				font-size: 30upx;width: 80px;margin-right: 0px;" @getphonenumber="decryptPhoneNumber"
						class="open-button getInfo bun">获取手机</button>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					所在地区
				</view>
				<view class="input" style="display: flex; justify-content: space-between;" @tap="onChangeAddress">
					{{model.locationAddress?model.locationAddress:"请选择"}}
					<uni-icons type="location-filled" color="#31b977" size="24"></uni-icons>
				</view>

			</view>
			<view class="row">
				<view class="nominal">
					详细地址
				</view>
				<view class="input">
					<textarea v-model="model.addressDetail" auto-height="true" placeholder="输入详细地址"></textarea>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					设置默认地址
				</view>
				<view class="input switch">
					<switch color="#62c980" :checked="model.defaultFlag" @change="onSwitchChange" />
				</view>
			</view>
		</view>
		<view class="save">
			<view class="button"  @click="addUserAddress()" style="margin-bottom: 10px;">
				保存地址
			</view>
			<view class="del" v-if="model._id" @tap="deleteAddress">
				删除收货地址
			</view>
		</view>
	</view>
</template>

<script>
	var WXBizDataCrypt = require('@/common/WXBizDataCrypt.js');
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	import {
		mapState,
		mapGetters
	} from 'vuex'
	export default {
		computed: {
			...mapGetters({
				hasLogin: 'hasLogin',
				userInfo: 'userInfo'
			})
		},
		components: {
			uniIcons
		},
		data() {
			return {
				model: {
					userName: "",
					phone: "",
					_id:"",
					longitude: "",
					latitude: "",
					defaultFlag: 1,
					locationAddress: "", //地图定位获得的地址
					addressDetail: "", //手动填写的地址
				}
			}
		},
		onLoad(e) {
			if (e && e.item) {
				this.model = JSON.parse(e.item)
			} else {
				//定位地址
				uni.getLocation({
					success: data => {
						console.log(data)
						this.model.longitude = data.longitude
						this.model.latitude = data.latitude
						uni.request({
							url: 'https://restapi.amap.com/v3/geocode/regeo?location=' + data
								.longitude +
								',' + data.latitude +
								'&key=739917e6ef56e4e27e30921366cc1d95',
							success: (data) => {
								console.log(data)
								this.model.locationAddress = data.data.regeocode.formatted_address
							}
						})
					}
				})
			}
		},
		methods: {
			deleteAddress(){
				let self=this;
				let {_id}=this.model
				uni.showModal({
					title: '删除',
					    content: '确定删除这个地址',
					    success: function (res) {
						if (res.confirm) {
							self.$api("address",'del',{_id}).then(res=>{
								self.$msg("删除成功");
								uni.navigateBack()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			onSwitchChange(e) {
				this.model.defaultFlag = e.detail.value;
			},
			decryptPhoneNumber(e) {
				let pc = new WXBizDataCrypt(this.$appid, this.userInfo.sessionKey); //wxXXXXXXX为你的小程序APPID
				let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
				this.model.phone = data.phoneNumber
			},
			onChangeAddress() {
				let self = this;
				uni.chooseLocation({
					success: function(res) {
						self.model.locationAddress = res.name
						self.model.latitude = res.latitude
						self.model.longitude = res.longitude
					},
					fail: function(err) {
						console.log(err)
					}
				});
			},
			radioChange(evt) {
				this.model.sex = evt.detail.value;
			},
			addUserAddress() {
				let data = this.model
				if (data._id && data._id != "") {
					this.$api("address", "upd", data).then(res => {
						this.$msg("保存成功");
						uni.navigateBack({
							delta: 1
						})
					})
				} else {
					if (this.model.userName == "") {
						this.$msg("联系人不能为空");
						return false
					}
					if (this.model.phone == "") {
						this.$msg("电话不能为空");
						return false
					}
					if (this.model.locationAddress == "") {
						this.$msg("定位地址不能为空");
						return false
					}

					if (this.model.addressDetail == "") {
						this.$msg("备注地址不能为空");
						return false
					}
					this.$api("address", "add", data).then(res => {
						this.$msg("保存成功");
						uni.navigateBack({
							delta: 1
						})
					})
				}

			},
			updateUserAddress() {
				let data = this.model
				if (this.model.userName == "") {
					this.$msg("联系人不能为空");
					return false
				}
				if (this.model.phone == "") {
					this.$msg("电话不能为空");
					return false
				}
				if (this.model.locationAddress == "") {
					this.$msg("定位地址不能为空");
					return false
				}
				updateUserAddress(data).then(res => {
					if (res.returnCode == "2") {
						this.$msg("保存成功");
						uni.navigateBack({
							delta: 1
						})
					} else {
						this.$msg(res.description)
					}
				})
			},
			getLocationInfo(location) {
				let self = this;
				reverseGeocoder(location).then(res => {
					console.log('当前位置信息：', res)
					if (res.result) {
						self.model.districtCode = res.result.ad_info.adcode
					}

				}).catch(err => {

				})
			},


		}
	}
</script>
<style lang="scss">
	.save {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 320upx;

		.button {
			width: 90%;
			margin: auto;
			text-align: center;
			display: block;
			margin-bottom: 20px;
			background-image: linear-gradient(-90deg,
					#3da180 0%,
					#62c980 100%),
				linear-gradient(#e8ecef,
					#e8ecef);
			background-blend-mode: normal,
				normal;
			border-radius: 25px;

			height: 45px;
			line-height: 45px;
			font-family: PingFangSC-Medium;
			font-size: 14px;
			letter-spacing: 0px;
			color: #ffffff;
		}
	}

	.content {
		display: flex;
		flex-wrap: wrap;

		view {
			display: flex;
		}

		.row {
			width: 94%;

			margin: 0 3%;
			border-top: solid 1upx #eee;

			.nominal {
				width: 30%;
				height: 120upx;
				font-weight: 200;
				font-size: 30upx;
				align-items: center;
			}

			.input {
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 30upx;

				&.switch {
					justify-content: flex-end;
				}

				.textarea {
					margin: 20upx 0;
					min-height: 120upx;
				}
			}

			
		}
		
	}
	.del {
		
		width: 90%;
		margin: auto;
		text-align: center;
		display: block;
		margin-bottom: 20px;
		border-radius: 25px;
		height: 45px;
		line-height: 45px;
		font-family: PingFangSC-Medium;
		font-size: 14px;
		letter-spacing: 0px;
		color: #f06c7a;
		background-color: rgba(255, 0, 0, 0.1);
	}
</style>
